<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

abstract class Log
{
	/**
	 * Log file
	 *
	 * @var string
	 */
	static private $_filename;

	/**
	 * Set log filename
	 *
	 * @param string $filename
	 * @return void
	 */
	static protected function set($filename)
	{
		self::$_filename = (string) $filename;
	}

	/**
	 * Add message to log
	 *
	 * @param string $message
	 * @return void
	 */
	static public function add($message = NULL)
	{
		// log filename must be set
		if (!self::$_filename) return;

		// remove unnecessary characters
		$message = trim(str_replace(array("\n","\r","\t"), NULL, $message));

		// file path
		$path = dirname(__FILE__).'/../'.cfg::DIR_LOGS.self::$_filename;

		// username of member
		$member = NULL;

		// members can be logged only after the Member class initialization
		if (class_exists('Member')) {
			// set username
			$member = Member::getUsername();
		}

		// final format of log message
		$record = date('Y-m-d H:i:s',time())."<%>"._ip()."<%>"._hsc($member)."<%>"._hsc($message)."\n";

		// save log only if the message is not empty
		if (strlen(trim($message)) > 0) {
			// append log to a file
			file_put_contents($path, $record, FILE_APPEND | LOCK_EX);
		}
	}

	/**
	 * Erase log
	 *
	 * @return void
	 */
	static public function clear()
	{
		@unlink(cfg::DIR_LOGS.self::$_filename);
	}
}

// include Accesslog
require_once dirname(__FILE__).'/logs/lib.accesslog.php';

// include Actionlog
require_once dirname(__FILE__).'/logs/lib.actionlog.php';
